from rest_framework import mixins
from rest_framework.generics import GenericAPIView
from rest_framework.viewsets import GenericViewSet

from ERP_5.utils.base_views import MultipleDestroyMixin
from ERP_5.utils.paginations import GlobalPagination
from erp_system.models import UserModel
from erp_system.serializers.user_serializer import UserAddSerializer, UserUpdateDeleteSerializer, UserGetSerializer, \
    ResetPasswordSerializer


class RegisterUserView(mixins.CreateModelMixin, GenericViewSet):
    """
    create:
    用户注册。

    并且把用户注册之后的用户信息返回
    """
    queryset = UserModel.objects.all()
    serializer_class = UserAddSerializer


class UserView(mixins.RetrieveModelMixin,
               mixins.UpdateModelMixin,
               mixins.DestroyModelMixin,
               mixins.ListModelMixin,
               MultipleDestroyMixin,  # 批量删除用户
               GenericViewSet):
    """
    没有用户注册和修改用户密码的模型类
    """
    queryset = UserModel.objects.all()
    pagination_class = GlobalPagination

    def get_serializer_class(self):
        if self.action == 'partial_update' or self.action == 'update' or self.action == 'destroy':
            return UserUpdateDeleteSerializer
        else:
            return UserGetSerializer


class ResetPasswordView(mixins.UpdateModelMixin, GenericAPIView):
    """
    patch:
    用户--重置密码

    用户重置密码, status: 200(成功), return: None
    """
    queryset = UserModel.objects.all()
    serializer_class = ResetPasswordSerializer

    def patch(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)
